package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.MainActivity;
import com.dexilaonian.R;
import com.dexilaonian.adpter.MyStoreOrderAdapter;
import com.dexilaonian.bean.SaleOrderBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Logutil;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class MyStoreOrderAcitivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_title_service3)
    TextView baseTitleService3;
    @Bind(R.id.order_list)
    ListView orderList;
    @Bind(R.id.more_time)
    TextView moreTime;
    private List<SaleOrderBean> storelist = new ArrayList<>();
    private ArrayList<SaleOrderBean> saleOrderBeen;
    private MyStoreOrderAdapter myStoreOrderAdapter;
    private TextView store_cancle;
    private String order_id ="";
    private TextView store_pay;
    private LinearLayout store_detail;

    @Override
    public int getLayoutID() {
        return R.layout.activity_my_store_order_acitivity;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        baseTitleService3.setText("我的订单");
        initStore();
    }

    private void initStore() {
        showProgressBar(true);
        String storelist_url = Constant.base_url + "/sale_order/list.do";
        Map<String, String> storelist_map = new HashMap<>();
        storelist_map.put("limit", 20 + "");
        storelist_map.put("page", 1 + "");
        Okhttp.postString(true, storelist_url, storelist_map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
                showToast(state);
            }
            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Logutil.e("TTTTMYSTORE===" + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16) {
                        Intent intent = new Intent(MyStoreOrderAcitivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                    if (status == 0) {
                        String data = jsonObject.optString("data");
                        JSONObject jsonObject1 = new JSONObject(data);
                        String items = jsonObject1.optString("items");
                        saleOrderBeen = JsonUtil.json2beans(items, SaleOrderBean.class);
                        storelist.clear();
                        storelist.addAll(saleOrderBeen);
                        myStoreOrderAdapter = new MyStoreOrderAdapter(MyStoreOrderAcitivity.this, storelist);
                        orderList.setAdapter(myStoreOrderAdapter);
                        orderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
                                showToast(position+"");
                                order_id = saleOrderBeen.get(position).getOrder_id();
                                myStoreOrderAdapter.setOrderId(order_id);
                                myStoreOrderAdapter.notifyDataSetChanged();
                             /*   store_cancle = (TextView) orderList.getChildAt(position).findViewById(R.id.item_store_cancle);
                                store_pay = (TextView) orderList.getChildAt(position).findViewById(R.id.item_store_pay);
                                store_detail = (LinearLayout) orderList.getChildAt(position).findViewById(R.id.ll_store_detail);
                                storeCancle(order_id,position);*/
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
                showToast(state);
            }
        });
    }

    private void storeCancle(String order_id, final int postion) {

        String item_cancle_url = Constant.base_url + "/sale_order/cancel.do";
        Map<String, String> item_cancle_map = new HashMap<String, String>();
        item_cancle_map.put("OrderId", order_id);
         Okhttp.postString(true,item_cancle_url, item_cancle_map, new Okhttp.CallBac() {
          @Override
          public void onError(Call call, Exception e, String state, int id) {
              showToast(state);
          }

          @Override
          public void onResponse(String response, int id) {
              try {
                  JSONObject jsonObject = new JSONObject(response);
                  int status = Integer.parseInt(jsonObject.optString("status"));
                  if (status == 0) {
                      showToast("取消成功");
                      storelist.remove(postion);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              myStoreOrderAdapter.notifyDataSetChanged();
          }

          @Override
          public void onNoNetwork(String state) {
              showToast(state);
          }
      });

    }


    @OnClick({R.id.back, R.id.more_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.more_time:
                Intent intent = new Intent(MyStoreOrderAcitivity.this, MainActivity.class);
                intent.putExtra("user", 2);
                startActivity(intent);
                break;
        }
    }
}
